import _ from 'lodash';
import { t } from '@src/locales'

export const getFixedColumns = () => {
  return [
    {
      label: t('common.label.customer'),
      displayName: t('common.label.customer'),
      fieldName: 'name',
      field: 'name',
      show: true,
      fixed: true,
      isSystem: true,
      tableName: 'customer',
      minWidth: '150px',
      export: true,
    },
    {
      label: t('common.label.customerTag'),
      displayName: t('common.label.customerTag'),
      fieldName: 'customerTag',
      field: 'customerTag',
      tableName: 'customer',
      isSystem: true,
      fixed: true,
      show: true,
      export: true,
    },
    {
      label: t('common.label.customerSerialNumber'),
      displayName: t('common.label.customerSerialNumber'),
      field: 'serialNumber',
      fieldName: 'serialNumber',
      tableName: 'customer',
      isSystem: true,
      // width: '150px',
      fixed: true,
      show: true,
      export: true,
      sortable: 'custom',
    },
    {
      label: t('common.label.contact'),
      displayName: t('common.label.contact'),
      field: 'lmName',
      fieldName: 'lmName',
      tableName: 'customer',
      isSystem: true,
      // width: '100px',
      show: true,
      export: true,
    },
    {
      label: t('common.label.phone'),
      displayName: t('common.label.phone'),
      field: 'lmPhone',
      fieldName: 'lmPhone',
      isSystem: true,
      tableName: 'customer',
      minWidth: '150px',
      show: true,
      export: true,
    },
    {
      label: t('common.label.email'),
      displayName: t('common.label.email'),
      field: 'lmEmail',
      fieldName: 'lmEmail',
      isSystem: true,
      tableName: 'customer',
      minWidth: '150px',
      show: true,
      export: true
    },
    {
      label: t('common.label.area'),
      displayName: t('common.label.area'),
      field: 'customerAddress',
      fieldName: 'customerAddress',
      isSystem: true,
      tableName: 'customer',
      // width: '180px',
      show: true,
    },
    {
      label: t('common.label.addressDetail'),
      displayName: t('common.label.addressDetail'),
      field: 'detailAddress',
      fieldName: 'detailAddress',
      isSystem: true,
      tableName: 'customer',
      // width: '160px',
      show: true,
      export: true,
    },
    {
      label: t('common.label.serviceDepartment'),
      displayName: t('common.label.serviceDepartment'),
      field: 'tags',
      fieldName: 'tags',
      isSystem: true,
      tableName: 'customer',
      // width: '110px',
      show: true,
      export: true,
    },
    {
      label: t('common.label.customerManager'),
      displayName: t('common.label.customerManager'),
      field: 'customerManagerName',
      fieldName: 'customerManagerName',
      isSystem: true,
      tableName: 'customer',
      show: true,
      width: '110px',
      export: true,
    },
    {
      label: t('common.label.customerSynergies'),
      displayName: t('common.label.customerSynergies'),
      field: 'synergies',
      fieldName: 'synergies',
      formType: 'user',
      isSystem:true,
      tableName:'customer',
      show: true,
      width: '110px'
    },
    {
      label: t('common.label.enableOrDisabled'),
      displayName: t('common.label.enableOrDisabled'),
      field: 'status',
      fieldName: 'status',
      show: true,
      align: 'center',
      tableName: 'customer',
      isSystem: true,
      width: '100px',
      export: true,
    },
    {
      label: t('common.label.createTime'),
      displayName: t('common.label.createTime'),
      field: 'createTime',
      formType: 'datetime',
      fieldName: 'createTime',
      isSystem: true,
      tableName: 'customer',
      show: true,
      sortable: 'custom',
      width: '150px',
      export: true,
    },
    {
      label: t('common.label.updateTime'),
      displayName: t('common.label.updateTime'),
      field: 'updateTime',
      formType: 'datetime',
      fieldName: 'updateTime',
      isSystem: true,
      tableName: 'customer',
      show: true,
      sortable: 'custom',
      width: '150px',
      export: true,
    },
    {
      label: t('common.label.createUser'),
      displayName: t('common.label.createUser'),
      fieldName: 'createUser',
      field: 'createUser',
      tableName: 'customer',
      isSystem: true,
      // width: '80px',
      show: true,
      export: true,
    },
    {
      label: t('common.label.remindCount'),
      displayName: t('common.label.remindCount'),
      field: 'remindCount',
      fieldName: 'remindCount',
      tableName: 'customer',
      isSystem: true,
      // width: '80px',
    },
    {
      label: t('common.form.type.parentCustomer'),
      displayName: t('common.form.type.parentCustomer'),
      fieldName: 'parentCustomer',
      field: 'parentCustomer',
      tableName: 'customer',
      isSystem: true,
      show: true,
      export: true,
    },
  ];
};
export const getSelfFields = () => {
  return [
    {
      displayName: t('common.label.area'),
      fieldName: 'area',
      formType: 'area',
      isExport: false,
      isNull: 1,
      isSystem: 1,
      orderId: 10,
    },
    {
      displayName: t('common.label.addressDetail'),
      fieldName: 'addressDetail',
      formType: 'text',
      isExport: false,
      isNull: 1,
      isSystem: 1,
      orderId: 11,
    },
    {
      displayName: t('common.label.contact'),
      fieldName: 'linkmanId',
      formType: 'linkman',
      isExport: false,
      isNull: 1,
      isSystem: 1,
      orderId: 3,
      placeholder: t('common.placeholder.inputKeywordSearchContact'),
    },
    {
      displayName: t('common.label.createUser'),
      fieldName: 'createUser',
      formType: 'user',
      isExport: false,
      isNull: 1,
      isSystem: 1,
      setting: {
        isMulti: false
      },
      orderId: 12,
    },
    {
      displayName: t('common.label.createTime'),
      fieldName: 'createTime',
      formType: 'datetime',
      isExport: false,
      isNull: 1,
      isSystem: 1,
      operator: 'between',
      orderId: 98,
    },
    {
      displayName: t('common.label.updateTime'),
      fieldName: 'updateTime',
      formType: 'datetime',
      isExport: false,
      isNull: 1,
      isSystem: 1,
      operator: 'between',
      orderId: 99,
    },
    {
      displayName: t('common.label.hasRemind'),
      fieldName: 'hasRemind',
      formType: 'select',
      isExport: false,
      isNull: 1,
      isSystem: 1,
      operator: 'between',
      orderId: 96,
      setting: {
        isMulti: false,
        dataSource: [
          {
            text: t('common.base.hasState[0]'),
            value: 1,
          },
          {
            text: t('common.base.hasState[1]'),
            value: 0,
          },
        ],
      },
    },
    {
      displayName: t('common.label.status'),
      fieldName: 'status',
      formType: 'select',
      isExport: false,
      isNull: 1,
      isSystem: 1,
      operator: 'between',
      orderId: 97,
      setting: {
        isMulti: false,
        dataSource: [
          {
            text: t('common.base.enableState[0]'),
            value: 1,
          },
          {
            text: t('common.base.enableState[1]'),
            value: 0,
          },
        ],
      },
    },
  ];
};

export const matchOperator = field => {
  let formType = field.formType;
  let operator = '';

  switch (formType) {
  case 'date': {
    operator = 'between';
    break;
  }
  case 'datetime': {
    operator = 'between';
    break;
  }
  case 'select': {
    if (field.setting && field.setting.isMulti) {
      operator = 'contain';
    } else {
      operator = 'eq';
    }
    break;
  }
  case 'cascader': {
    operator = 'cascader';
    break;
  }
  case 'user': {
    operator = 'user';
    break;
  }
  case 'address': {
    operator = 'address';
    break;
  }
  case 'location': {
    operator = 'location';
    break;
  }
  case 'related_task': {
    operator = 'array_eq';
    break;
  }
  case 'formula': {
    operator = 'eq';
    break;
  }
  case 'related_customers':
    operator = 'in';
    break;
  default: {
    operator = 'like';
    break;
  }
  }
  return operator;
};

export const formatFields = fields => {
  let f = {};
  const selfFields = getSelfFields();
  return (
    [...fields, ...selfFields]
      // .filter(f => (f.isSearch || f.isSystem) && f.fieldName !== 'qrcodeId' && f.displayName !== '服务团队')
      .filter(
        f =>
          (f.isSearch || f.isSystem)
          && f.formType !== 'separator'
          && f.fieldName !== 'name'
          && f.fieldName !== 'lmPhone'
          && f.fieldName !== 'customerAddress'
          && f.fieldName !== 'lmName'
      )
      .map(field => {
        f = _.cloneDeep(field);

        let formType = f.formType;

        if (formType === 'datetime') {
          formType = 'date';
        }

        // 客户formType === user只单选
        if (formType === 'user') {
          if(f.setting) {
            f.setting.isMultiple = 0;
            f.setting.isMulti = false;
          } else {
            f.setting = { 
              isMultiple: 0,
              isMulti: false,
            }
          }
        }

        if (f.fieldName === 'customer') {
          formType = 'customer';
        }

        if (f.fieldName === 'synergies') {
          f.setting.isMulti = true;
        }

        if (f.fieldName === 'manager') {
          f.setting.isMulti = false;
          f.fieldName = 'customerManager';
        }

        // TODO 国际化 服务团队、服务部门判断
        if (f.fieldName === 'tags') {
          formType = 'tags';
          f.setting.isMulti = false;
        }

        return Object.assign({
          ...f,
          isNull: 1,
          formType,
          originalFormType: f.formType,
          orderId: f.isSystem ? f.orderId - 100 : f.orderId,
          operator: matchOperator(f),
        });
      })
      .sort((a, b) => a.orderId - b.orderId)
  );
};
